/**
 * 
 */
package tests;

import hibernateUtils.HibernateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Iterator;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Before;
import org.junit.Test;

import subscribersManagement.Entitled;
import subscribersManagement.Subscriber;
import subscribersManagement.SubscriberDAO;






import static org.junit.Assert.*;


import exceptions.BadParametersException;
import exceptions.LentMediaException;
import exceptions.SubscriberExistsException;


import exceptions.MediaExistsException;


import loansManagement.Loan;
import main.Library;
import booksManagement.Book;
import dvdsManagement.Dvd;
import specimensManagement.Specimen;
import mediasManagement.Media;
import mediasManagement.MediaDAO;


/**
 * @author Fabre, Malric
 *
 */
public class NewTests {

	private Library lib;
	private Media b,b1,b2,b3,b4;
	private Media d;
	private EntityManager em;
	private Subscriber s;
	private Entitled e;

	private ArrayList<String> authors;

	@Before
	public void beforeTests() throws BadParametersException, MediaExistsException
	{
		lib = new Library();

		authors = new ArrayList<String>();
		authors.add("Antony");
		b = new Book("Fundamentals of Database System", new GregorianCalendar(),  "0-321-20228-4", 3, authors);
		b1 = new Book("Fundamentals of Database Systems1", new GregorianCalendar(),  "0-321-20228-1", 4, authors);
		b2 = new Book("Fundamentals of Database Systems2", new GregorianCalendar(),  "0-321-20228-3", 10, authors);
		b3 = new Book("Fundamentals of Database Systems3", new GregorianCalendar(),  "0-321-20228-2", 2, authors);
		d= new Dvd("Dvd2", new GregorianCalendar(),  "0-555-20228-4", 3, authors);

		s= new Subscriber("Ambroise","Malric", new GregorianCalendar(1987, Calendar.JULY, 10), new GregorianCalendar(2012, Calendar.MAY, 02));
	}

	@Test
	public void addMediasTest() 
	{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();
		mediasManagement.MediaDAO.add(d);
		mediasManagement.MediaDAO.add(b);
		mediasManagement.MediaDAO.add(b1);
		t.commit();
		session.close();
	}
	
	@Test
	public void addSpecimenMediasTest() 
	{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();
		mediasManagement.MediaDAO.addSpecimen(b1);
		t.commit();
		session.close();
	}
	
	@Test
	public void removeSpecimenMediasTest() 
	{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();
		mediasManagement.MediaDAO.removeSpecimen(b1);
		t.commit();
		session.close();
	}
	
	@Test
	public void removeMediasTest() 
	{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();
		mediasManagement.MediaDAO.remove(d);
		t.commit();
		session.close();
	}
	
	@Test
	public void addSubscriberTest() 
	{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();
		subscribersManagement.SubscriberDAO.add(s);
		t.commit();
		session.close();
	}
	
	@Test
	public void removeSubscriberTest() 
	{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();
		subscribersManagement.SubscriberDAO.remove(s);
		t.commit();
		session.close();
	}
	
	@Test
	public void testLoanPersistence1() throws BadParametersException, MediaExistsException, SubscriberExistsException{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(b);
		lib.addSubscriber(s);
		t.commit();
		HibernateUtil.closeSession();
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		Media x= mediasManagement.MediaDAO.get(b.getId());
		t.commit();
		HibernateUtil.closeSession();
		System.out.println(x.toString());
		try {
			Specimen ex= new Specimen(b);
			session.save(ex);		
			session.save(s);
			
		} catch (Exception e) {
			System.out.println("error ajout specimen media");
		}
		t.commit();
		session.close();
	}
	
}
